<template>
  <div class="login">
    <div class="container">
      <div class="avatar">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <el-form
        :model="user"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入用户名"
            @focus="verification('username')"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
            @focus="verification('password')"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="login-btn" @click="handlerLogin"
        >登录</el-button
      >
    </div>
  </div>
</template>

<script>
import { login } from "@/apis/user.js";

export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^1\d{4}$|^1\d{10}$/,
            message: "请输入合法的手机号",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 表达获取焦点的时候触发 校验规则
    verification(prop) {
      // 获取表单的值  clearValidate 框架里面的一个方法
      this.$refs.ruleForm.clearValidate(prop);
    },
    handlerLogin() {
      // 在用户登录的时候先验证。如果数据不和发则阻止本次请求
      // 表单有一个 validate() 方法可以进行用户数据的验证，所采取的规则就是rules 规则
      // 他有一个参数是一个回调函数，当验证结果的时候，会调用这个函数并调用函数并传一个验证结果，结果为true，说明验证通过，则验证失败
      this.$refs.ruleForm.validate(async (valid) => {
        // 判断 如果通过验证规则就发送请求 则提示用户
        if (valid) {
          let res = await login(this.user);
          if (res.data.message == "登录成功") {
            localStorage.setItem("heimahoutai", JSON.stringify(res.data.data));
            this.$router.push({ path: "/index" });
          }
          // 提示用户
          this.$message({
            showClose: true,
            message: res.data.message,
            type: "success",
          });
        } else {
          // 提示用户
          this.$message({
            showClose: true,
            message: "请输入正确的用户名和密码",
            type: "error",
          });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 150px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>